package com.itheima.admin.controller;

import com.itheima.admin.dto.AdChannelPageRequestDto;
import com.itheima.admin.pojo.AdChannel;
import com.itheima.admin.service.AdChannelService;
import com.itheima.common.enums.HttpCodeEnum;
import com.itheima.common.vo.PageResultVo;
import com.itheima.common.vo.ResultVo;
import com.itheima.core.controller.AbstractCoreController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @version 1.0
 * @description 说明
 * @package com.itheima.admin.controller
 */
@RestController
@RequestMapping("/channel")
@Api(tags = "频道管理")
public class AdChannelController extends AbstractCoreController<AdChannel> {

    @Autowired
    private AdChannelService adChannelService;

    @Autowired
    public AdChannelController(AdChannelService adChannelService){
        super(adChannelService);
    }

    /**
     * 频道分页查询
     * @param dto
     * @return
     */
    @PostMapping("/list")
    @ApiOperation("频道分页搜索")
    public PageResultVo<AdChannel> findPage(@RequestBody AdChannelPageRequestDto dto){
        PageResultVo<AdChannel> result = adChannelService.findPage(dto);
        return result;
    }

    /**
     * 添加频道
     * @param adChannel
     * @return
     */

    @PostMapping("/save")
    @ApiOperation("添加频道")
    public ResultVo insert(@RequestBody AdChannel adChannel){
        adChannelService.add(adChannel);
        return ResultVo.ok();
    }

    /**
     * 通过id查询，已抽取，此处删除
     * @param id
     * @return
     */

    /*@GetMapping("/{id}")
    @ApiOperation("通过id查询")
    public ResultVo<AdChannel> findById(@PathVariable(name = "id") int id){
        AdChannel adChannel = adChannelService.getById(id);
        return ResultVo.ok(adChannel);
    }*/

    /**
     * 更新数据
     * @param adChannel
     * @return
     */

    @PostMapping("/update")
    @ApiOperation("更新频道")
    public ResultVo updateByPrimaryKey(@RequestBody AdChannel adChannel){
        if(adChannel.getId()==null){
            return ResultVo.error(HttpCodeEnum.PARAM_REQUIRE);
        }
        boolean success = adChannelService.updateById(adChannel);
        if(!success){
            return ResultVo.error();
        }
        return ResultVo.ok();
    }

    /**
     * 删除频道，已抽取，此处删除
     * @param id
     * @return
     */

    /*@ApiOperation("通过id删除")
    @DeleteMapping("/del/{id}")
    public ResultVo deleteById(@PathVariable(name = "id") int id){
        if(adChannelService.removeById(id)){
            return ResultVo.ok();
        }
        return ResultVo.error();
    }*/
}
